package com.example.campus_activity_backend.controller;

import com.example.campus_activity_backend.common.BaseResponse;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.common.ResultUtil;
import com.example.campus_activity_backend.domain.Activity;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @PostMapping("/add")
    public BaseResponse<Long> addActivity(@RequestBody Activity activity){
        if(activity == null){
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long id = activityService.addActivity(activity);
        return ResultUtil.success(id);
    }

    @PutMapping("/post/{id}")
    public BaseResponse<Boolean> postActicity(@PathVariable int id){
        boolean result = activityService.postActivity(id);
        return ResultUtil.success(result);
    }

    @GetMapping("/{activityName}/{status}")
    public BaseResponse<List<Activity>> getActivityByName(@PathVariable String activityName
            ,@PathVariable Integer status){
        if(activityName == null || status == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Activity> activities = activityService.getActivityByName(activityName, status);
        return ResultUtil.success(activities);
    }
}
